//
//  JWBoJianGiftScrollView.m
//  jinwang
//
//  Created by jzb on 2018/4/8.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWBoJianGiftScrollView.h"
#import "ELCVFlowLayout.h"
#import "UIImage+GIF.h"
@interface JWBoJianGiftScrollView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_collectionView;
    
}
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;

@end
@implementation JWBoJianGiftScrollView

static NSString *identifier = @"collection_cell";

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [UIColor redColor];
        
    }
    return self;
}
-(void)setGiftListArr:(NSMutableArray *)giftListArr{
    _giftListArr = giftListArr;
    [self scrollViewBuild];
}
- (void)scrollViewBuild{
    
     ELCVFlowLayout *horizontalLayout = [[ELCVFlowLayout alloc] init];
    
//    //垂直方向的
//    layout.minimumLineSpacing = 0;
//    //    水平方向的
//    layout.minimumInteritemSpacing = 0;
    
    //3.设置滚动方向
    /*
     UICollectionViewScrollDirectionVertical,  垂直方向
     UICollectionViewScrollDirectionHorizontal  水平方向
     */
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:horizontalLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];

    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.bounces = NO;
    _collectionView.pagingEnabled = YES;
    
    [self addSubview:_collectionView];
    
//    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self);
//    }];
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
    [self addSubview:_collectionView];
}

#pragma mark --UICollectionViewDataSource

//单元格个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _giftListArr.count;
    
}
//单元格内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    JWBoJianGiftModel *model = _giftListArr[indexPath.item];
    
    //使用注册的单元格类 创建单元格
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    self.iconImageView = [[UIImageView alloc] initWithFrame:CGRM(45, 20, 750/4-90, 750/4-80)];
    self.iconImageView.backgroundColor = [UIColor clearColor];
    NSString *str2 = [model.gift_img substringFromIndex:model.gift_img.length-3];//截取掉下标3之后的字符串
    
    if ([str2 isEqualToString:@"gif"] || [str2 isEqualToString:@"GIF"] ) {
        _iconImageView.image = [UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.gift_img]]];
    }else{
        [_iconImageView sd_setImageWithURL:[NSURL URLWithString:model.gift_img] placeholderImage:[UIImage imageNamed:@""]];
    }

    [cell.contentView addSubview:self.iconImageView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRM(0, 750/4-60, 750/4, 40)];
    
    label.backgroundColor = [UIColor clearColor];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.font = [UIFont systemFontOfSize:12];
    
    label.text = [NSString stringWithFormat:@"¥ %@",model.gift_price];
    [cell.contentView addSubview:label];
    
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    return cell;
    
    
}
// 定义每个UICollectionViewCell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kScreenWidth) / 4, self.frame.size.height/2);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    JWBoJianGiftModel *model = _giftListArr[indexPath.item];

    self.GiftBlcok(model);
}

#pragma mark ----UICollectionViewDelegateFlowLayout
//单元格距离collectionView 四周边缘的距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}
@end
